/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeesite.modules.st.entity.StCustomRecord;
import com.jeesite.modules.st.entity.StProductRecord;
import com.jeesite.modules.st.entity.StSendNoticeBillDetail;
import com.jeesite.modules.st.service.StCustomRecordService;
import com.jeesite.modules.st.service.StProductRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.st.entity.StSendNoticeBill;
import com.jeesite.modules.st.service.StSendNoticeBillService;

import java.util.List;

/**
 * 送货通知Controller
 *
 * @author HOHD
 * @version 2020-12-19
 */
@Controller
@RequestMapping(value = "${adminPath}/st/stSendNoticeBill")
public class StSendNoticeBillController extends BaseController {

    @Autowired
    private StSendNoticeBillService stSendNoticeBillService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public StSendNoticeBill get(String id, boolean isNewRecord) {
        return stSendNoticeBillService.get(id, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("st:stSendNoticeBill:view")
    @RequestMapping(value = {"list", ""})
    public String list(StSendNoticeBill stSendNoticeBill, Model model) {
        model.addAttribute("stSendNoticeBill", stSendNoticeBill);
        return "modules/st/stSendNoticeBillList";
    }

    @RequiresPermissions("st:stSendNoticeBill:view")
    @RequestMapping(value = {"forChoose"})
    public String forChoose(StSendNoticeBill stSendNoticeBill, Model model) {
        model.addAttribute("stSendNoticeBill", stSendNoticeBill);
        return "modules/st/stSendNoticeBillList-4-choose";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("st:stSendNoticeBill:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<StSendNoticeBill> listData(StSendNoticeBill stSendNoticeBill, HttpServletRequest request, HttpServletResponse response) {
        stSendNoticeBill.setPage(new Page<>(request, response));
        Page<StSendNoticeBill> page = stSendNoticeBillService.findPage(stSendNoticeBill);
        return page;
    }

    @RequiresPermissions("st:stSendNoticeBill:view")
    @RequestMapping(value = "findDetail")
    @ResponseBody
    public List<StSendNoticeBillDetail> findDetail(StSendNoticeBill stSendNoticeBill) {
        StSendNoticeBillDetail stSendNoticeBillDetail = new StSendNoticeBillDetail();
        stSendNoticeBillDetail.idIn(stSendNoticeBill.getId().split(","));
        List<StSendNoticeBillDetail> list = stSendNoticeBillService.findDetail(stSendNoticeBillDetail);
        return list;
    }

    @Autowired
    StCustomRecordService stCustomRecordService;
    @Autowired
    StProductRecordService stProductRecordService;
    ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("st:stSendNoticeBill:view")
    @RequestMapping(value = "form")
    public String form(StSendNoticeBill stSendNoticeBill, Model model) throws JsonProcessingException {
        model.addAttribute("stSendNoticeBill", stSendNoticeBill);
        List<StCustomRecord> customList = stCustomRecordService.findList(new StCustomRecord());
        model.addAttribute("customList", customList);

        List<StProductRecord> productList = stProductRecordService.findList(new StProductRecord());
        model.addAttribute("productList", objectMapper.writeValueAsString(productList));
        return "modules/st/stSendNoticeBillForm";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("st:stSendNoticeBill:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated StSendNoticeBill stSendNoticeBill) {
        stSendNoticeBillService.save(stSendNoticeBill);
        return renderResult(Global.TRUE, text("保存送货通知成功！"));
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("st:stSendNoticeBill:edit")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(StSendNoticeBill stSendNoticeBill) {
        stSendNoticeBillService.delete(stSendNoticeBill);
        return renderResult(Global.TRUE, text("删除送货通知成功！"));
    }

}